<template>
	<view class="bg-white Afont padding-20 overhid" style="height: 100vh;"
		:style="{background:'url('+Imgbase+'/brave_goodpoor_bgc.png)','background-size':'100%',}">

		<view class="padding-top-50 padding-left-20 ">
			<view class="">
				<image class="w-80 h-80" :src="Imgbase + '/brave_goodindex_img1.png'" mode="aspectFit" @click="back">
				</image>

			</view>
		</view>

		<view class="w100 h-1400 margin-auto black-color font-24">

			<view class="padding-top-50">

				<view class=" padding-left-10 black-color font-30 font-weight-500 padding-bottom-15">
					默认地址
				</view>
				<view v-for="(item,index) in addlist" :key="index" class="w100 h-200 margin-auto padding-35"
					:style="{background:'url('+Imgbase+'/brave_goodmy_img1.png)','background-size':'100% 100%',}"
					v-if="item.selected == 1">
					<view class="flex-between padding-left-10 padding-top-10 fff font-28">
						<view class="">
							<text class="font-weight-500   margin-right-20"
								style="border-radius: 8rpx;">{{item.name}}</text>
							{{item.phone}}
						</view>
						<view class="df alc" @click="modifyadd(index)">
							编辑
							<!-- <image class="w-40 h-25 margin-top-5" :src="Imgbase + '/new_goodaddress_img4.png'"
								mode="aspectFit"></image> -->
						</view>
					</view>
					<view class="w100 one-line padding-left-10 fff font-26 padding-top-18">
						{{item.detail}}
					</view>
					<view class="fff font-20 padding-right-15" style="text-align: right;" @click="deletes(item.id)">
						删除
						<image class="w-30 h-16 margin-top-20" :src="Imgbase + '/new_goodaddress_img3.png'"
							mode="aspectFit"></image>
					</view>
				</view>
			</view>

			<view class="padding-top-50">

				<view class=" padding-left-10 black-color font-30 font-weight-500 padding-bottom-15">
					其他地址
				</view>

				<scroll-view scroll-y class="w100 h-800" scroll-with-animation style="overflow: hidden;">

					<view v-for="(item,index) in addlist" :key="index"
						class="w100 h-200 margin-auto padding-35 margin-bottom-25"
						:style="{background:'url('+Imgbase+'/brave_goodmy_img1.png)','background-size':'100% 100%',}"
						v-if="item.selected == 0">
						<view class="flex-between padding-left-10 padding-top-10 fff font-28">
							<view class="">
								<text class="font-weight-500   margin-right-20"
									style="border-radius: 8rpx;">{{item.name}}</text>
								{{item.phone}}
							</view>
							<view class="df alc" @click="modifyadd(index)">
								编辑
								<!-- <image class="w-40 h-25 margin-top-5" :src="Imgbase + '/new_goodaddress_img4.png'"
								mode="aspectFit"></image> -->
							</view>
						</view>
						<view class="w100 one-line padding-left-10 fff font-26 padding-top-18">
							{{item.detail}}
						</view>
						<view class="fff font-20 padding-right-15" style="text-align: right;" @click="deletes(item.id)">
							删除
							<image class="w-30 h-16 margin-top-20" :src="Imgbase + '/new_goodaddress_img3.png'"
								mode="aspectFit"></image>
						</view>
					</view>
				</scroll-view>


			</view>
		</view>


		<view class="btn w100 h-200 text-center  text-center">
			<view class="img">
				<u-button class="w-500" color="#506EB8"  @click="addresst" hairline text="添加新地址"></u-button>
			</view>
		</view>


		<view class="h-100">

		</view>

		<!-- 添加地址 -->
		<view class="">
			<u-popup :show="addshow" @close="addclose" :safeAreaInsetBottom="false" :round="30">
				<view class="addto">
					<view class="close" @click="addclose">
						<image class="w-40 h-40" :src="Imgbase + '/new_goodaddress_img1.png'" mode="aspectFit"></image>
					</view>
					<view class="" style="height: 80rpx;">

					</view>
					<view class="item" v-for="(item,index) in itemlist" :key="index">
						<view class="left">
							{{item.text}}
						</view>

						<view class="right">
							<input v-if="item.prs == '手机号'" style="width: 95%;color: #000;font-weight: bold;"
								:placeholder="item.prs" v-model="item.data" type="number" maxlength="11">
							<input v-else style="width: 95%;color: #000;font-weight: bold;" :placeholder="item.prs"
								v-model="item.data" type="text">
						</view>
					</view>

					<view class="items">
						<view class="left">
							设置为默认收货地址
						</view>

						<view class="right padding-6" @click="change">
							<view class="w100 h100 radiusb-50" style="background-color: #973F23;" v-if="value">

							</view>
						</view>
					</view>
					<view class="btns" style="" v-if="modify ==0">
						<image @click="addresson" class="w-400 h-100 margin-auto"
							:src="Imgbase + '/new_goodaddress_img2.png'" mode="aspectFit"></image>
					</view>
					<view class="btns" style="" v-if="modify ==1">
						<u-button text="删除" customStyle="height:70rpx;width:300rpx;border-radius: 20rpx;"
							@click="deletes" color="linear-gradient(to right, rgb(203, 203, 203), rgb(172, 172, 172))">
						</u-button>
						<view class="">
							<image @click="addresson" class="w-300 h-80" :src="Imgbase + '/new_goodaddress_img2.png'"
								mode="aspectFit"></image>
						</view>
					</view>
				</view>
			</u-popup>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				Imgbase: getApp().globalData.Imgbase,
				addlist: [],
				show: false,
				itemlist: [{
						text: '收货人',
						prs: '姓名',
						data: ''
					},
					{
						text: '手机号码',
						prs: '手机号',
						data: ''
					},
					{
						text: '详细地址',
						prs: '省、市、区、街道',
						data: ''
					},
				],
				value: false,
				selected: 0,
				id: '',
				modify: 0,
				addshow: false
			}
		},
		onShow() {
			this.addressList()
		},
		onLoad(options) {},
		methods: {
			// 添加收获地址
			addresst() {
				this.addshow = true
				this.value = false
				this.itemlist.forEach(function(e, i) {
					e.data = ''
				})
			},
			addclose() {
				this.addshow = false
			},
			back() {
				uni.navigateBack()
			},
			// 删除地址
			deletes(e) {
				let _this = this;
				uni.showModal({
					title: '提示',
					content: '是否确认删除该地址',
					success: function(res) {
						if (res.confirm) {
							uni.request({
								url: _this.$globals.apibase + '/api/Luckorder/deleteAddress',
								data: {
									token: uni.getStorageSync('token'),
									id: e ? e : _this.id,
								},
								method: 'POST',
								header: {
									'content-type': 'application/json'
								},
								success: (res) => {
									_this.addressList()
									_this.addshow = false
									_this.$refs.uToast.show({
										message: "地址删除成功",
									})
								}
							});
						} else if (res.cancel) {}
					}
				});
			},
			opens() {
				this.show = true
				this.id = ''
				this.itemlist.forEach(function(item, index) {
					item.data = ''
				})
				this.selected = 0
				this.value = false
				// console.log('open');
			},
			open() {

			},
			close() {
				this.show = false
				// console.log('close');
			},
			change(e) {
				this.value = !this.value
				if (this.value) {
					this.selected = 1
				} else {
					this.selected = 0
				}
			},
			// 编辑地址
			modifyadd(idx) {
				// 判断是否为编辑
				this.modify = 1
				this.addshow = true
				this.itemlist[0].data = this.addlist[idx].name
				this.itemlist[1].data = this.addlist[idx].phone
				this.itemlist[2].data = this.addlist[idx].detail
				this.id = this.addlist[idx].id
				console.log(this.addlist[idx].selected)
				if (this.addlist[idx].selected == 1) {
					this.selected = 1
					this.value = true
				} else {
					this.selected = 0
					this.value = false
				}
			},
			// 保存收货地址
			addresson() {
				let _this = this
				if (_this.itemlist[0].data == '') {
					this.$refs.uToast.show({
						message: "请填写收货人姓名",
					})
					return
				}
				if (_this.itemlist[1].data.length != 11) {
					this.$refs.uToast.show({
						message: "请填写正确的手机号码",
					})
					return
				}
				if (_this.itemlist[2].data == '') {
					this.$refs.uToast.show({
						message: "请填写详细地址",
					})
					return
				}

				uni.request({
					url: _this.$globals.apibase + '/api/Luckorder/operaAddress',
					data: {
						token: uni.getStorageSync('token'),
						name: _this.itemlist[0].data,
						phone: _this.itemlist[1].data,
						detail: _this.itemlist[2].data,
						selected: _this.selected,
						id: _this.id,
					},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						if (res.data.code == '1') {
							_this.addressList()
							_this.addshow = false
							if (_this.id == 0) {
								this.$refs.uToast.show({
									message: "添加成功",
								})
							} else {
								this.$refs.uToast.show({
									message: "修改成功",
								})
							}
						}
						_this.itemlist.forEach(function(item, index) {
							item.data = ''
						})
						_this.selected = 0
						_this.value = false
					}
				});
			},
			// 获取地址列表
			addressList() {
				let _this = this;
				uni.request({
					url: _this.$globals.apibase + '/api/Luckorder/addressList',
					data: {
						token: uni.getStorageSync('token'),
					},
					method: 'POST',
					header: {
						'content-type': 'application/json'
					},
					success: (res) => {
						_this.addlist = res.data.data.list;
						// for (let i = 0; i < res.data.data.list.length; i++) {
						// 	_this.addresslist = _this.addresslist.concat(res.data.data.list[i])
						// }
						// if (_this.address == null) {
						// 	_this.address = res.data.data.list2[0]
						// 	_this.address_id = res.data.data.list2[0].id
						// }
					}
				});
			},
		},
	}
</script>

<style lang="less" scoped>
	.addto {
		height: 780rpx;
		background-color: #E7D2C2;
		border-radius: 50rpx;

		.items {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 20rpx 30rpx 0;
			margin-bottom: 50rpx;

			.left {
				font-size: 34rpx;
				font-weight: bold;
				color: #973F23;
			}

			.right {
				width: 38rpx;
				height: 38rpx;
				background-color: #efefef;
				border: solid 1rpx #973f23;
				border-radius: 19rpx;
			}
		}

		.close {
			float: right;
			padding-top: 30rpx;
			padding-right: 35rpx;
		}

		.item {
			display: flex;
			padding: 30rpx;

			.left {
				min-width: 180rpx;
				font-size: 34rpx;
				font-weight: bold;
				padding-top: 10rpx;
				color: #973F23;
			}

			.right {
				width: 500rpx;
				padding: 15rpx;
				border-radius: 25rpx;
				background-color: #CDBAAC;
			}
		}
	}

	.btns {
		width: 85%;
		margin: 0 auto;
		border-radius: 50rpx;
		overflow: hidden;
	}

	.btns {
		width: 85%;
		margin: 0 auto;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.btn {
		position: fixed;
		bottom: 20rpx;
		left: 17.5%;
		z-index: 99;

		.img {
			width: 65%;
			
		}
	}

	.black {
		background-color: #353941;
	}

	.org {
		background-color: #FC8924;
	}
</style>